package com.example.frn.service.impl;

import com.example.frn.dao.*;
import com.example.frn.dto.PageDTO;
import com.example.frn.dto.ReviewPageDto;
import com.example.frn.entity.RankingList;
import com.example.frn.entity.Review;
import com.example.frn.service.ReviewService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ReviewServiceImpl implements ReviewService {
    @Autowired
    private ReviewMapper reviewMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private FilmMapper filmMapper;
    @Autowired
    private LikeMapper likeMapper;
    @Override
    public boolean save(Review review) {
        review.setStatus(0);
        return reviewMapper.insert(review)>0;
    }

    @Override
    public boolean updateStatus(Integer id, Integer status) {
        return reviewMapper.updateStatus(id,status)>0;
    }

    @Override
    public boolean updateLikeNum(Integer uid, Integer rid) {
        if(likeMapper.count(uid,rid)==0){
            likeMapper.insert(uid, rid);
            return reviewMapper.updateLikeNum(rid,1)>0;
        }
        else {
            likeMapper.delete(uid, rid);
            return reviewMapper.updateLikeNum(rid,-1)>0;
        }
    }

    @Override
    public boolean removeById(Integer id) {

        return reviewMapper.deleteById(id)>0;
    }

    @Override
    public Review getById(Integer id) {
        Review review = reviewMapper.selectById(id);
        review.setUser(userMapper.selectById(review.getUserId()));
        review.setFilm(filmMapper.selectById(review.getFilmId()));
        return review;
    }

    @Override
    public PageInfo<Review> page(ReviewPageDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        List<Review> reviewList = reviewMapper.selectList(dto.getFilmId(),dto.getUserId(),dto.getStatus());
       /* for (Review review : reviewList) {
            review.setUser(userMapper.selectById(review.getUserId()));
            PageHelper.startPage(1,5);
            review.setReplyList(reviewMapper.selectListByReviewId(review.getId()));
        }*/
        return new PageInfo<>(reviewList);
    }
}
